import request from '../utils/request'

// 获取社团列表
export function getClubs(params) {
  return request({
    url: '/activity/clubs/',
    method: 'GET',
    params
  })
}

// 获取活动列表
export function getActivities(params) {
  return request({
    url: '/activity/activities/',
    method: 'GET',
    params
  })
}

// 获取社团详情
export function getClubDetail(id) {
  return request({
    url: `/activity/clubs/${id}/`,
    method: 'GET'
  })
}

// 创建社团
export function createClub(data) {
  // 创建FormData对象来处理文件上传
  const formData = new FormData()
  
  // 将所有字段添加到FormData中
  Object.keys(data).forEach(key => {
    // 只有值存在且不为空字符串或null时才添加到FormData
    if (data[key] !== '' && data[key] !== null && data[key] !== undefined) {
      formData.append(key, data[key])
    }
  })
  
  // 不需要手动设置Content-Type，浏览器会自动处理
  return request({
    url: '/activity/clubs/create/',
    method: 'POST',
    data: formData
  })
}

// 获取用户社团列表
export function getUserClubs() {
  return request({
    url: '/activity/user/clubs/',
    method: 'GET'
  })
}

// 创建活动
export function createActivity(data) {
  return request({
    url: '/activity/activities/create/',
    method: 'POST',
    data
  })
}

// 获取活动详情
export function getActivityDetail(id) {
  return request({
    url: `/activity/activities/${id}/`,
    method: 'GET'
  })
}

// 活动报名
export function signUpActivity(data) {
  return request({
    url: '/activity/activities/registration/',
    method: 'POST',
    data
  })
}